package net.maergaiyun.controller;


import net.maergaiyun.controller.request.LinkGroupAddRequest;
import net.maergaiyun.controller.request.LinkGroupUpdateRequest;
import net.maergaiyun.enums.BizCodeEnum;
import net.maergaiyun.model.LinkGroupDO;
import net.maergaiyun.service.LinkGroupService;
import net.maergaiyun.util.JsonData;
import net.maergaiyun.vo.LinkGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/group/v1")
public class LinkGroupController {
    @Autowired
    private LinkGroupService linkGroupService;

    /**
     * 添加分组
     *
     * @param addRequest
     * @return
     */
    @PostMapping("add")
    public JsonData add(@RequestBody LinkGroupAddRequest addRequest) {
        int rows = linkGroupService.add(addRequest);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAIL);
    }

    /**
     * 删除分组
     *
     * @param groupId
     * @return
     */
    @PostMapping("del/{group_id}")
    public JsonData del(@PathVariable("group_id") Long groupId) {
        int rows = linkGroupService.del(groupId);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST);
    }

    /**
     * 分组详情
     *
     * @param groupId
     * @return
     */
    @PostMapping("detail/{group_id}")
    public JsonData detail(@PathVariable("group_id") Long groupId) {
        LinkGroupVO linkGroupVO = linkGroupService.detail(groupId);
        return JsonData.buildSuccess(linkGroupVO);
    }

    @PostMapping("list")
    public JsonData list() {
        List<LinkGroupVO> linkGroupVOList = linkGroupService.list();
        return JsonData.buildSuccess(linkGroupVOList);
    }

    @PostMapping("update")
    public JsonData update(@RequestBody LinkGroupUpdateRequest request) {
        int rows = linkGroupService.updateById(request);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_OPER_FAIL);
    }
}
